package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqUpdatePassword;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.uikit.CustomDialog;
import com.lanlian.utillib.ToastUtils;
import com.lanlian.utillib.WorksSizeCheckUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 11:55
 *     desc   : 修改交易密码
 *     version: 1.0
 * </pre>
 */
public class UpdatePasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtPasswordOld;
    private EditText mEtPasswordNew;
    private EditText mEtPasswordConfirm;
    private TextView mTvOk;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password, true);
        initView();
        setListeners();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("修改密码");
        mEtPasswordOld = findViewById(R.id.et_password_old);
        mEtPasswordNew = findViewById(R.id.et_password_new);
        mEtPasswordConfirm = findViewById(R.id.et_password_confirm);
        mTvOk = findViewById(R.id.tv_ok);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvOk.setOnClickListener(this);
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvOk);
        worksSizeCheckUtil.addAllEditText(mEtPasswordConfirm, mEtPasswordNew, mEtPasswordOld);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    mTvOk.setBackgroundResource(R.mipmap.login_btn);
                } else {
                    mTvOk.setBackgroundResource(R.drawable.bg_click_false);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_ok:
                String oldPassword = mEtPasswordOld.getText().toString();
                String newPassword = mEtPasswordNew.getText().toString();
                String confirmPassword = mEtPasswordConfirm.getText().toString();
                if (TextUtils.isEmpty(oldPassword)) {
                    ToastUtils.show(this, "请输入旧密码");
                    return;
                }
                if (TextUtils.isEmpty(newPassword)) {
                    ToastUtils.show(this, "请输入新密码");
                    return;
                }
                if (TextUtils.isEmpty(confirmPassword)) {
                    ToastUtils.show(this, "请确认新密码");
                    return;
                }
                if (!confirmPassword.equals(newPassword)) {
                    ToastUtils.show(this, "两次输入的密码不一致");
                    return;
                }
                //修改密码
                ReqUpdatePassword reqUpdatePassword = new ReqUpdatePassword();
                reqUpdatePassword.setPwd(newPassword);
                reqUpdatePassword.setOldPwd(oldPassword);
                showLoadingDialog();
                VolleyRequestUtils.getInstance().updatePassword(this.toString(), reqUpdatePassword, new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        dismissLoadingDialog();
                        new CustomDialog(UpdatePasswordActivity.this, new CustomDialog.OnClickListener() {
                            @Override
                            public void onLeftClick() {
                            }

                            @Override
                            public void onRightClick() {
                                mDBManager.setLoginOut();
                                EventBusUtils.post(new EventMessage(EventCode.EVENT_LOGIN_OUT_REFRESH));
                                finish();
                            }
                        }).setOneBtn(true).setTitle("提示").setContent("密码修改成功，需要重新登录").show();
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        dismissLoadingDialog();
                        showToast(baseResponse.getMsg());
                    }
                });
                break;
            default:
                break;
        }
    }
}
